﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;

namespace SimpleLiteForm
{
    
    public class PresentationManager
    {
        private const string FILE_PATH = "presentations.txt";
        private int _numOfPresentation = 0;
        private int _currentPresentationIndex = 0;

        private List<Presentation> _presentations;

        public PresentationManager()
        {
            _presentations = new List<Presentation>();
            readFile();
        }

        private void readFile()
        {
            //file not exist, create file
            string filePath = FILE_PATH;
            if(File.Exists(filePath))
            {
                //parse file
                using (StreamReader fileReader = new System.IO.StreamReader(FILE_PATH))
                {
                    string line;

                    line = fileReader.ReadLine();
                    if (!Int32.TryParse(line, out _numOfPresentation))
                    {
                        initializeFile();
                        return;
                    }
                    fileReader.ReadLine();//get the return line
                    readPresentations(fileReader);

                    fileReader.Close();
                }
            }
            else
            {
                //create file and initialize
                initializeFile();
            }
        }

        public void endCurrentPresentation()
        {
            _presentations[_currentPresentationIndex].setEndTime(DateTime.Now);
            rewriteFile();
        }

        public void addScreenshotPath(string path)
        {
            _presentations[_currentPresentationIndex].addScreenshotPath(path);
            //rewrite
            rewriteFile();
        }

        private void initializeFile()
        {
            _numOfPresentation = 0;
            using(StreamWriter fileWriter = new System.IO.StreamWriter(FILE_PATH))
            {
                fileWriter.WriteLine(_numOfPresentation.ToString());
                fileWriter.WriteLine();
                fileWriter.Close();
            } 
        }

        private void readPresentations(StreamReader reader)
        {
            for(int index = 0;index<_numOfPresentation;index++)
            {
                //get title
                string title = reader.ReadLine();
                //get time
                DateTime date = DateTime.Parse(reader.ReadLine());
                DateTime beginTime = DateTime.Parse(reader.ReadLine());
                DateTime endTime = DateTime.Parse(reader.ReadLine());

                Presentation pres = new Presentation(title, date);
                pres.setBeginEnd(beginTime, endTime);

                int numOfScreenshots = Int32.Parse(reader.ReadLine());
                for (int index1 = 0; index1 < numOfScreenshots;index1++ )
                {
                    pres.readScreenshotPath(reader.ReadLine());
                }
                    //get the return
                reader.ReadLine();

                _presentations.Add(pres);
            }
        }

        public DataTable displayRecord()
        {
            DataTable dataTable = new DataTable();
            //add columns
            dataTable.Columns.Add("Title");
            dataTable.Columns.Add("Date");

            for(int index = 0;index<_numOfPresentation;index++)
            {
                string[] record = _presentations[index].getRecord();
                dataTable.Rows.Add(record);
            }
            return dataTable;
        }

        public Presentation getCurrentPresentation()
        {
            return _presentations[_currentPresentationIndex];
        }

        public Presentation getPresentation(int index)
        {
            return _presentations[index];
        }

        public void addPresentation(string title, DateTime dt)
        {
            Presentation pres = new Presentation(title,dt);
            _presentations.Add(pres);
            _currentPresentationIndex = _numOfPresentation;
            _numOfPresentation++;

            rewriteFile();
            
        }

        private void rewriteFile()
        {
            //rewrite file
            using (StreamWriter fileWriter = new StreamWriter(FILE_PATH, false))
            {
                fileWriter.WriteLine(_numOfPresentation);
                fileWriter.WriteLine();

                for (int index = 0; index < _numOfPresentation; index++)
                {
                    _presentations[index].writeFile(fileWriter);
                }

                fileWriter.Close();
            }
        }
    }
}
